package com.server.service;

import com.server.meta.ClassMeta;
import com.server.util.PackageScan;
import com.server.util.Utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 14-2-13
 * Time: 上午10:26
 * To change this template use File | Settings | File Templates.
 */
public class ServiceImplGeneratorService {
    /**
     * 生成所有
     * @throws Exception
     */
    public void generateServiceImpls(GeneratorParams generatorParams) {
        String importPackage = generatorParams.getPackageName();
        if(generatorParams.getPackageName()==null){
            generatorParams.setPackageName("com."+ Utils.getSplitLast(generatorParams.getPath(), "/"));
            importPackage=generatorParams.getPackageName();
        }
        generatorParams.setPackageName(generatorParams.getPackageName()+".service.impl");
        String classPackage = generatorParams.getClassPackage();
        String packageName = generatorParams.getPackageName();
        Set<Class<?>> classes = PackageScan.getClasses(classPackage);
        for (Class<?> clazz : classes) {
            ClassMeta classMeta = new ClassMeta();
            classMeta.setPackageName(packageName);
            classMeta.setClassName(clazz.getSimpleName());
            classMeta.setImportPackage(importPackage);
            classMeta.setLowerClassName(clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1));
            Map root = new HashMap();
            root.put("bean", classMeta);
            generatorParams.setTemplateName("serviceImpl");
            generatorParams.setRoot(root);
            generatorParams.setFilename(classMeta.getClassName() + "ServiceImpl");
            try {
                new GeneratorService().generate(generatorParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * single
     * @param generatorParams
     */
    public void generateServiceImpl(GeneratorParams generatorParams)  {
        String importPackage = generatorParams.getPackageName();
        if(generatorParams.getPackageName()==null){
            generatorParams.setPackageName("com."+ Utils.getSplitLast(generatorParams.getPath(), "/"));
            importPackage=generatorParams.getPackageName();
        }
        generatorParams.setPackageName(generatorParams.getPackageName()+".service.impl");
        String packageName = generatorParams.getPackageName();
        Class<?> clazz =generatorParams.getBeanClass();
        ClassMeta classMeta = new ClassMeta();
            classMeta.setPackageName(packageName);
            classMeta.setClassName(clazz.getSimpleName());
            classMeta.setImportPackage(importPackage);
            Map root = new HashMap();
            root.put("bean", classMeta);
            generatorParams.setTemplateName("serviceImpl");
            generatorParams.setRoot(root);
            generatorParams.setFilename(classMeta.getClassName() + "ServiceImpl");
            try {
                new GeneratorService().generate(generatorParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
